<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82882996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\piles;

use think\facade\Db;
use app\common\dao\piles\PilesPlanDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;
/**
 * Class PilesPlanRepository
 *
 * @mixin PilesPlanDao
 */
class PilesPlanRepository extends BaseRepository
{

    public function __construct(PilesPlanDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'propertyInfo' => function ($query) {
                    $query->field('id,name');
                    $query->bind(['property_name' => 'name']);
                },
            ])
            ->order('id', 'desc')
            ->select();
        return compact('count', 'list');
    }


    public function addInfo(array $data, int $propertyId, int $userId)
    {
        return Db::transaction(function () use ($data, $propertyId, $userId) {
            if (isset($data['stair_price']) && is_array($data['stair_price'])) {
                $data['stair_price'] = json_encode($data['stair_price'], JSON_UNESCAPED_UNICODE);
            }
            $data['property_id'] = $propertyId;
            $info = $this->dao->create($data);
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($propertyId, $userId, 'PILES.PLAN.ADD', $data);
            return $info;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            if (isset($data['stair_price']) && is_array($data['stair_price'])) {
                $data['stair_price'] = json_encode($data['stair_price'], JSON_UNESCAPED_UNICODE);
            }
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'PILES.PLAN.EDIT', $data);
            return $this->dao->update($info['id'], $data);
        });
    }


    public function getDetail(int $id)
    {
        $data = $this->dao->search([])
            ->with([
                'propertyInfo' => function ($query) {
                    $query->field('id,name');
                    $query->bind(['property_name' => 'name']);
                },
            ])
            ->where('id', $id)
            ->find();
        return $data;
    }


    public function batchDelete(array $ids, int $userId = 0, string $remark = '')
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ], 'id,property_id,plan_name');
        if ($list) {
            $num = 0;
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId, $remark);
                $num++;
            }
            return $num;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0, string $remark = '')
    {
        return Db::transaction(function () use ($info, $userId, $remark) {
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'PILES.PLAN.DEL', $info);
            $this->dao->delete($info['id']);
        });
    }

    public function getEnergyPlanData(int $propertyId = null, array $where = [])
    {
        $list = $this->dao->search($where, $propertyId)
            ->field('plan_name label,id value')
            ->select()->toArray();
        return $list;
    }
}